﻿#region 命名空间

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;
using WinnerCMS.Model.Enum;

#endregion

namespace WinnerCMS.WebUI.Admin.Common
{
    [Access("OP")]
    public partial class Maintenance : WebFactory.PageBase
    {
        private byte ModelId;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.ModelId = byte.Parse(Request["ModelId"]);
            if (!IsPostBack)
            {
                new SpecialManage().BindDDL(this.ddlSpecial, BindOption.NoLink, "<请选择>", this.ModelId);
                var myBll = new CategoryManage();
                myBll.BindDDL(this.ddlCategory, BindOption.NoLink, "<请选择>", this.ModelId);
                myBll.BindDDL(this.ddlCategorySearch, BindOption.NoLink, "<全部栏目>", this.ModelId);
            }
        }

        protected void btnSelectOne_Click(object sender, EventArgs e)
        {
            var count = this.lblLeftList.Items.Count;
            var index = 0;
            for (var i = 0; i < count; i++)
            {
                var item = this.lblLeftList.Items[index];
                if (this.lblLeftList.Items[index].Selected)
                {
                    this.lblLeftList.Items.Remove(item);
                    this.lblRightList.Items.Add(item);
                    index--;
                }
                index++;
            }
        }

        protected void btnSelectAll_Click(object sender, EventArgs e)
        {
            var list = new ListItem[this.lblLeftList.Items.Count];
            for (var i = 0; i < this.lblLeftList.Items.Count; i++)
            {
                list[i] = this.lblLeftList.Items[i];
            }

            this.lblRightList.Items.AddRange(list);
            this.lblLeftList.Items.Clear();
        }

        protected void btnReMoveOne_Click(object sender, EventArgs e)
        {
            var count = this.lblRightList.Items.Count;
            var index = 0;
            for (var i = 0; i < count; i++)
            {
                var item = this.lblRightList.Items[index];
                if (this.lblRightList.Items[index].Selected)
                {
                    this.lblRightList.Items.Remove(item);
                    this.lblLeftList.Items.Add(item);
                    index--;
                }
                index++;
            }
        }

        protected void btnReMoveAll_Click(object sender, EventArgs e)
        {
            var list = new ListItem[this.lblRightList.Items.Count];
            for (var i = 0; i < this.lblRightList.Items.Count; i++)
            {
                list[i] = this.lblRightList.Items[i];
            }
            this.lblLeftList.Items.AddRange(list);
            this.lblRightList.Items.Clear();
        }


        protected void btnMoveToNewSpecial_Click(object sender, EventArgs e)
        {
            if (this.cbk使用ID.Checked == false)
            {
                var IDs = new MyStringBuilder();
                foreach (ListItem li in this.lblRightList.Items)
                {
                    IDs.Append(li.Value + ",");
                }
                IDs.TrimEnd();

                if (string.IsNullOrEmpty(IDs.ToString()))
                {
                    MsgBox.Message("请选择文章", this.UpdatePanel1);
                    return;
                }

                new CoreManage().MoveToNewSpecial(IDs.ToString(), this.ddlSpecial.SelectedValue, this.UpdatePanel1);
            }
            else
            {
                if (string.IsNullOrEmpty(this.txtIDs.Text))
                {
                    MsgBox.Message("请输入ID", this.UpdatePanel1);
                    return;
                }
                new CoreManage().MoveToNewSpecial(this.txtIDs.Text, this.ddlSpecial.SelectedValue, this.UpdatePanel1);
            }
        }

        protected void btnMoveToNewClass_Click(object sender, EventArgs e)
        {
            var IDs = new MyStringBuilder();
            foreach (ListItem li in this.lblRightList.Items)
            {
                IDs.Append(li.Value + ",");
            }
            IDs.TrimEnd();


            new CoreManage().MoveToNewCategory(IDs.ToString(), this.ddlCategory.SelectedValue, this.UpdatePanel1);

            this.txtKeyword.Text = "";
            this.lblLeftList.Items.Clear();
            this.lblRightList.Items.Clear();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            var pp = new PagingParam
            {
                PageNo = 1,
                PageSize = int.MaxValue
            };
            var list = new CoreManage().CategoryPageBindList(int.Parse(this.ddlCategorySearch.SelectedValue),
                this.txtKeyword.Text, pp);
            this.lblLeftList.DataSource = list;
            this.lblLeftList.DataTextField = "ContentTitle";
            this.lblLeftList.DataValueField = "ID";
            this.lblLeftList.DataBind();

            if (this.lblLeftList.Items.Count < 1)
            {
                MsgBox.Message("查找不到数据", this.UpdatePanel1);
            }
        }

        protected void btnReplace_Click(object sender, EventArgs e)
        {
            var IDs = new MyStringBuilder();
            foreach (ListItem li in this.lblRightList.Items)
            {
                IDs.Append(li.Value + ",");
            }
            IDs.TrimEnd();
            var res = new CoreManage().Replace(this.txtOldString.Text, this.txtNewString.Text, IDs.ToString(),
                this.cbkRegex.Checked);
            if (res.Errors.Length == 0)
            {
                MsgBox.Message("操作完成", this.UpdatePanel1);
            }
            else
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
            }
        }

        protected void cbk使用ID_CheckedChanged(object sender, EventArgs e)
        {
            //cbk使用选取方式.Checked = !cbk使用ID.Checked;
            //txtIDs.Enabled = cbk使用ID.Checked;
        }

        protected void btnUpData_Click(object sender, EventArgs e)
        {
            var datas = new List<dynamic>();
            if (this.cbkTemplateFileName.Checked)
                datas.Add(
                    new
                    {
                        Field = "TemplateFileName",
                        this.cbkTemplateFileName.Checked,
                        Data = this.txtTemplateFileName.Text.Trim()
                    });
            if (this.cbkStars.Checked)
                datas.Add(new {Field = "Stars", this.cbkStars.Checked, Data = this.ddlStars.SelectedValue});
            if (this.cbkElite.Checked)
                datas.Add(new {Field = "Elite", this.cbkElite.Checked, Data = this.RBL_Elite.Items[0].Selected});
            if (this.cbkOnTop.Checked)
                datas.Add(new {Field = "OnTop", this.cbkOnTop.Checked, Data = this.RBL_OnTop.Items[0].Selected});
            if (this.cbkAllowComment.Checked)
                datas.Add(
                    new
                    {
                        Field = "AllowComment",
                        this.cbkAllowComment.Checked,
                        Data = this.RBL_AllowComment.Items[0].Selected
                    });
            if (this.cbkPass.Checked)
                datas.Add(new {Field = "Pass", this.cbkPass.Checked, Data = this.RBL_Pass.Items[0].Selected});


            var IDs = new MyStringBuilder();
            foreach (ListItem li in this.lblRightList.Items)
            {
                IDs.Append(li.Value + ",");
            }
            IDs.TrimEnd();


            var res = new CoreManage().BatchUpData(datas, IDs.ToString());
            if (res.Errors.Length == 0)
            {
                MsgBox.Message("操作完成", this.UpdatePanel1);
            }
            else
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
            }
        }
    }
}